class Solution 
{
public:
    int trap(vector<int>& height) 
    {
        int n = height.size();
        int ans = 0;
        int left = 0, right = n - 1;
        int lmax = 0, rmax = 0;
        while(left < right)
        {
            lmax = max(lmax, height[left]);
            rmax = max(rmax, height[right]);
            if(height[left] < height[right])
                ans+= lmax - height[left++];
            else
                ans+= rmax - height[right--];
        }
        return ans;
    }
};

//  滑动窗口  接雨水问题   